﻿import QtQuick 2.0
import QtQuick.Window 2.0

Item {
    id: root
    width: Screen.width
    height: Screen.height

    property bool isOpened: false
    property var lastDate: new Date
    property var lastMinDate: new Date
    property string myTimeText: "10:00"
    property string myTime: myTimeText

    property int restTime: 10 * 60
    property int workTime: 50*60

    Rectangle{
        id: rect
        width: parent.width
        height: parent.height
        color: "black"
        y: 0

        Text {
            id: text
            anchors.centerIn: parent
            text: "你已经连续工作50分钟了，请休息一下\n" + myTime
            font.family: "微软雅黑"
            font.bold: true
            font.pointSize: 30
            color: "white"
            horizontalAlignment: Text.AlignHCenter
        }
    }

    ParallelAnimation{
        id: animDown
        PropertyAnimation{ target: rect; property: "y"; from: -Screen.height; to: 0; easing.type: Easing.Linear; duration: 500}

        onStopped: {
            lastMinDate = new Date

            myTime = myTimeText
            timerMin.restart()
            timer.stop()
        }
    }

    ParallelAnimation{
        id: animUp
        PropertyAnimation{ target: rect; property: "y"; from: 0; to: -Screen.height; easing.type: Easing.Linear; duration: 500}

        onStopped: {
            widget.hide()
        }
    }


    Timer{
        id: timer
        interval: 1000
        repeat: true
        running: true
        onTriggered: {
            var currentDate = new Date

            //秒到达50*60
            if(currentDate-lastDate > workTime*1000){
                widget.show()
                animDown.restart()
            }
        }
    }

    Timer{
        id: timerMin
        interval: 500
        repeat: true
        onTriggered: {
            var currentDate = new Date

            //流逝的时间
            var timeLapse = Math.floor(currentDate-lastMinDate) / 1000

            if(restTime-timeLapse>=0){
                var left = (restTime-timeLapse)

                var min = Number(left/60).toFixed(0)
                var sec = Number(left%60).toFixed(0)

                myTime = addZero(min) + ":" + addZero(sec)

            }
            else{
                lastDate = currentDate
                timerMin.stop()
                animUp.restart()
                timer.restart()
            }
        }
    }

    function addZero(value){
        if(Number(value)>=10){
            return value
        }
        else{
            return '0' + value
        }
    }
}

